/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.model.viewadpater;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.niclab.vanbus.R;
import org.niclab.vanbus.model.BusRoute;

import android.content.Context;
import android.content.res.ColorStateList;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BusRouteArrayAdapter extends ArrayAdapter<Entry<BusRoute,List<String>>>{
	
	//List<Entry<BusRoute,List<String>>> routesWithTimes;
	private static final String LOG_TAG="BusRouteArrayAdapter";
	
	private Context context;
	private int rowLayout;
	
	public BusRouteArrayAdapter(Context context, int layoutId, Map<BusRoute,List<String>> routesWithTimes ){
		super(context, layoutId, new ArrayList<Entry<BusRoute,List<String>>>(routesWithTimes.entrySet()));
		this.context = context;
		this.rowLayout = layoutId;
			
	}
	
	@Override
	public View getView (int position, View convertView, ViewGroup parent){
		View curView = convertView;
		
		if(curView ==null){
			LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//curView = LayoutInflater.from(this.context).inflate(this.rowLayout,parent,false);
			curView=li.inflate(this.rowLayout,parent,false);
			
		}
		
		Entry<BusRoute,List<String>> routeWithTimes = getItem(position);
		
		BusRoute route = routeWithTimes.getKey();
		List<String> times = routeWithTimes.getValue();
		
		TextView routeIdText = (TextView) curView.findViewById(R.id.bus_route_id_for_stop);
		routeIdText.setText(route.getRouteId());
		
		TextView routeDirText = (TextView) curView.findViewById(R.id.bus_route_dir_for_stop);
		routeDirText.setText(route.getRouteDir());
		
		TextView routeNameText = (TextView) curView.findViewById(R.id.bus_route_name_for_stop);
		String name=route.getRouteName();
		routeNameText.setText(name.substring(name.indexOf('-')+1));
		
		TextView routeTimesText = (TextView) curView.findViewById(R.id.bus_route_times_for_stop);
		routeTimesText.setText(TextUtils.join("  ", times.toArray()));
		
		//set the favorite icon
		ImageView favView = (ImageView)curView.findViewById(R.id.bus_route_favorite_for_stop);
		if(route.isFavorite()){
			favView.setImageResource(android.R.drawable.star_big_on);
		}else
			favView.setImageResource(android.R.drawable.star_big_off);
		
		
		//set refreshed animation
		if(route.isTimeScheduleRefreshed()){
			Log.v(LOG_TAG,"drawing the refresh animation");
			//final TextView refresh = (TextView)curView.findViewById(R.id.bus_route_times_for_stop);
			Animation anim= AnimationUtils.loadAnimation(context,R.anim.refresh_flash);
				
			/*anim.setAnimationListener(new AnimationListener(){

				@Override
				public void onAnimationEnd(Animation animation) {
					//refresh.setText("");
					//refresh.setTextColor(origColor);
					Log.d(LOG_TAG,"animation is done");
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					Log.d(LOG_TAG,"animation repetion ");
				
					
				}

				@Override
				public void onAnimationStart(Animation animation) {
					//refresh.setTextColor();
					// TODO Auto-generated method stub
					
				}
				
			});
			refresh.startAnimation(anim); */
			
			routeTimesText.startAnimation(anim);
		}
		return curView;
		
		
	}
	

}
